package vn.keho.module.dompeto.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

import com.bingkaimedia.merchantbeta.R;

import java.util.ArrayList;
import java.util.List;

import vn.keho.module.dompeto.HomeActivity;
import vn.keho.module.dompeto.model.ChannelModel;
import vn.keho.module.dompeto.rest.DompetoRequest;
import vn.keho.module.dompeto.sqlite.DompetoSqliteHelper;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {link vn.keho.module.dompeto.fragment.LeftFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class BlackFragment extends Fragment {
    public static final int BLACK_FRAGMENT_ID = 5;

    private ListView blackListView;

    private DompetoSqliteHelper dompetoSqliteHelper;
    private BlackListViewAdapter adapter;

    private List<ChannelModel> listData = new ArrayList<ChannelModel>();

    // TODO: Rename and change types and number of parameters
    public static BlackFragment newInstance() {
        BlackFragment fragment = new BlackFragment();

        return fragment;
    }

    public BlackFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dompetoSqliteHelper = new DompetoSqliteHelper(getActivity());
        adapter = new BlackListViewAdapter();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_black, container, false);
        blackListView = (ListView) view.findViewById(R.id.blackListView);
        blackListView.setAdapter(adapter);

        refreshData();
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

    }

    @Override
    public void onDetach() {
        super.onDetach();

    }

    public void refreshData() {
        listData = dompetoSqliteHelper.getListChannel(DompetoRequest.username);
        adapter.notifyDataSetChanged();
    }

    private class BlackListViewAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return listData.size();
        }

        @Override
        public Object getItem(int i) {
            return listData.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                /*LayoutInflater mInflater = (LayoutInflater) getActivity()
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.list_item_handle_left, null);*/

                convertView = LayoutInflater.from(parent.getContext()).inflate(
                        R.layout.list_item_black_box, null);
            }

            final ChannelModel channelModel = listData.get(position);

            TextView txtChannelTitle = (TextView) convertView.findViewById(R.id.txtChannelTitle);

            txtChannelTitle.setText(channelModel.getChanneltitle());

            CheckBox chkShowChannel = (CheckBox) convertView.findViewById(R.id.chkShowChannel);
            chkShowChannel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CheckBox chk = (CheckBox) view;

                    if(chk.isChecked()) {
                        int emptyIndex = HomeActivity.getInstance().findEmptySlot();
                        if(emptyIndex != AdapterView.INVALID_POSITION) {
                            HomeActivity.getInstance().restoreBlackBox(channelModel, emptyIndex);
                            dompetoSqliteHelper.removeChannel(DompetoRequest.username, channelModel.getChannelid());
                        } else {
                            chk.setChecked(false);
                        }
                    } else {
                        HomeActivity.getInstance().clearBlackBox(channelModel);
                        dompetoSqliteHelper.insertChannel(DompetoRequest.username, channelModel);
                    }
                }
            });

            return convertView;
        }
    }
}
